package com.ynwt.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by huangying on 2017/4/25.
 */
public class TimeUtil {


//    public final static int YEAR_SCOPE = 1;
//
//    public final static int MONTH_SCOPE = 2;
//
//    public final static int WEEK_SCOPE = 3;

    public final static int DAY_SCOPE = 4;

    /**
     * 判断某一时间是否在一个区间内
     *
     * @param sourceTime
     *            时间区间,半闭合,如[10:00-20:00)
     * @param curTime
     *            需要判断的时间 如10:00
     * @return
     * @throws IllegalArgumentException
     */
    public static boolean isInTime(String sourceTime, String curTime) {
        if (sourceTime == null || !sourceTime.contains("-") || !sourceTime.contains(":")) {
            throw new IllegalArgumentException("Illegal Argument arg:" + sourceTime);
        }
        if (curTime == null || !curTime.contains(":")) {
            throw new IllegalArgumentException("Illegal Argument arg:" + curTime);
        }
        String[] args = sourceTime.split("-");
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
        try {
            long now = sdf.parse(curTime).getTime();
            long start = sdf.parse(args[0]).getTime();
            long end = sdf.parse(args[1]).getTime();
            if (args[1].equals("00:00")) {
                args[1] = "24:00";
            }
            if (end < start) {
                if (now >= end && now < start) {
                    return false;
                } else {
                    return true;
                }
            }
            else {
                if (now >= start && now < end) {
                    return true;
                } else {
                    return false;
                }
            }
        } catch (ParseException e) {
            e.printStackTrace();
            throw new IllegalArgumentException("Illegal Argument arg:" + sourceTime);
        }
    }

    /**
     * 是否在某个时间段内[)，左闭右开区间
     * @param start
     * @param end
     * @param target
     * @param scope
     * @return
     */
    public static boolean inTime(Date start, Date end, Date target, int scope) {

        if (scope == TimeUtil.DAY_SCOPE) {
            //获取当天从0点开始点毫秒数
            long startTime = (start.getTime() + 28800000l) % 86400000l;
            long endTime = (end.getTime() + 28800000l) % 86400000l;
            long targetTime = (target.getTime() + 28800000l) % 86400000l;
//            System.out.println("startTime====" + startTime);
//            System.out.println("endTime====" + endTime);
//            System.out.println("targetTime====" + targetTime);
//            System.out.println(new Date(startTime));
//            System.out.println(new Date(endTime));
//            System.out.println(new Date(targetTime));
            return targetTime >= startTime && targetTime < endTime ? true : false;
        }
        return false;
    }




    /**
     * 判断所给日期是不是今天
     * @param date
     * @return
     */
    public static boolean isToday(Date date) {
        return isDateBefore(date, 0);
    }

    /**
     * 判断所给日期是不是昨天
     * @param date
     * @return
     */
    public static boolean isYesterday(Date date) {
        return isDateBefore(date, 1);
    }

    /**
     * 判断所给日期是不是明天
     * @param date
     * @return
     */
    public static boolean isTomorrow(Date date) {
        return isDateBefore(date, -1);
    }



    public static boolean isDateBefore (Date date, int n) {

        //获取n-1天前的开始时间
        Calendar end = startOfCalendarBaseNow(n - 1);
        //获取n+1天前的结束时间
        Calendar start = endOfCalendarBaseNow(n + 1);

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);

        //大于n+1天前的结束时间（大于等于n天前的开始时间）并且 小于n-1天前的开始时间（小于等于n天前的结束时间）
        return calendar.after(start)  && calendar.before(end) ? true : false;
    }


    /**
     * 获得本周第一天（星期一）零点时刻
     * @return
     */
    public static Date getMondayOfThisWeek(){
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DAY_OF_WEEK, -1);
        cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        Date date = cal.getTime();
        return date;
    }

    /**
     * 获取上周一零点的时刻
     * @return
     */
    public static Date getMondayOfLastWeek() {
        Date date = getMondayOfThisWeek();
        return new Date(date.getTime() - 604800000l);
    }

    /**
     * 获取N天前的那一天（0点）
     * @param n 距现在的天数
     * @return
     */
    public static Date startOfDateBaseNow(int n) {
        Date preDate = new Date(new Date().getTime() - n * 86400000l);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(preDate);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }

    /**
     * 获取n天前的开始时间
     * @param n
     * @return
     */
    public static Calendar startOfCalendarBaseNow(int n) {
        Date preDate = new Date(new Date().getTime() - n * 86400000l);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(preDate);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar;
    }

    /**
     * 获取n天前的结束始时间
     * @param n
     * @return
     */
    public static Calendar endOfCalendarBaseNow(int n) {
        Date preDate = new Date(new Date().getTime() - n * 86400000l);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(preDate);
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        calendar.set(Calendar.MILLISECOND, 999);
        return calendar;
    }

    /**
     * 获取n天前的结束始时间
     * @param n
     * @return
     */
    public static Date endOfDateBaseNow(int n) {
        Date preDate = new Date(new Date().getTime() - n * 86400000l);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(preDate);
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        calendar.set(Calendar.MILLISECOND, 999);
        return calendar.getTime();
    }
    /**
     * 获取指定日期对应的零点
     * @param date
     * @return
     */
    public static Date getZeroDate(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }

    /**
     * 获得本月第一天（星期一）零点时刻
     * @return     */
    public static Date getFirstDayOfThisMonth() {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.DAY_OF_MONTH, 1);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        Date date = cal.getTime();
        cal.set(Calendar.MONTH, 9);
        return date;
    }

    /**
     * 获取本周是今年的第几周（新一年的第一个周一开始，为第一周）
     * @param date
     * @return
     */
    public static int getWeekNumOfYear(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.get(Calendar.WEEK_OF_YEAR);
        calendar.add(Calendar.DAY_OF_WEEK, -1);

        return calendar.get(Calendar.WEEK_OF_YEAR);
    }
}
